package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hzit.dto.SpecificationDTO;
import com.hzit.entity.Spec;
import com.hzit.entity.Specification;
import com.hzit.entity.SpecificationOption;
import com.hzit.mapper.SpecificationMapper;
import com.hzit.mapper.SpecificationOptionMapper;
import com.hzit.service.ISpecificationService;
import javafx.scene.shape.Circle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class SpecificationServiceImpl implements ISpecificationService {
	@Autowired
	private SpecificationMapper specificationMapper;
	@Autowired
	private SpecificationOptionMapper optionMapper;

	//1. 查询规格列表
	@Override
	public List<SpecificationDTO> findAll() {
		List<Specification> specifications = specificationMapper.selectAll();
		return specifications.stream().map(m -> {
			SpecificationDTO dto = new SpecificationDTO();
			BeanUtil.copyProperties(m,dto,true);
			return dto;
		}).collect(Collectors.toList());
	}

	//2. 根据规格id查询规格组合对象
	@Override
	public Spec findById(Integer id) {
		//2.1 查询出规格
		Specification specification = specificationMapper.selectByPrimaryKey(id);
		//2.2 组织查询条件
		Example example = new Example(SpecificationOption.class);
		Example.Criteria criteria = example.createCriteria();
		if(id != null){
			criteria.andEqualTo("specId",id);
		}
		//2.3 规格规格id这个外键，查询出规格选项列表
		List<SpecificationOption> options = optionMapper.selectByExample(example);
		//2.4 返回规格选项及规格的组合对象
		return new Spec(specification, options);
	}

	//3.  添加规格
	@Override
	public void insert(Spec spec) {
		//3.1 添加规格
		Specification specification = spec.getSpecification();
		specificationMapper.insert(specification);
		//3.2 添加规格选项
		List<SpecificationOption> options = spec.getOptions();
		for (SpecificationOption option : options) {
			//3.2.1  绑定规格选项的规格id值
			option.setSpecId(specification.getId());
			//3.2.2 添加规格选项
			optionMapper.insert(option);
		}
	}

	//4. 修改规格及规格选项
	@Override
	public void update(Spec spec) {
		//4.1  修改规格
		Specification specification = spec.getSpecification();
		specificationMapper.updateByPrimaryKey(specification);
		//4.2  在规格选项表中，根据外键删除规格选项
		//4.2.1 设置要删除的条件
		Example example = new Example(SpecificationOption.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("specId",spec.getSpecification().getId());
		//4.2.2 根据条件删除
		optionMapper.deleteByExample(example);
		//4.3 重新添加规格选项
		for (SpecificationOption option : spec.getOptions()) {
			option.setSpecId(spec.getSpecification().getId());
			optionMapper.insert(option);
		}
	}

	//5. 删除规格及规格选项
	@Override
	public void delete(Integer id) {
		//5.1  先删除规格
		specificationMapper.deleteByPrimaryKey(id);
		//5.2  再删除规格选项
		//5.2.1 构造要删除的条件
		Example example = new Example(SpecificationOption.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("specId",id);
		//5.2.2 根据条件删除规格
		optionMapper.deleteByExample(example);
	}
}
